//
//  FQDLabOpinionHeadCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/16/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabOpinionHeadCell.h"
#import "FQDLabTitleImageView.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "DanmakuView.h"

@interface FQDLabOpinionHeadCell () <DanmakuDelegate>

@property (nonatomic, strong) FQDLabTitleImageView *titleImageView;
@property (nonatomic, strong) DanmakuView *danmakuView;
@property (nonatomic, strong) NSTimer *danmakuTimer;
@property (nonatomic, assign) CGFloat currentTime;
@property (nonatomic, strong) DanmakuConfiguration *danmakuConfig;
@property (nonatomic, assign) CGFloat maxTime;

@end

@implementation FQDLabOpinionHeadCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _currentTime = 0;
        [self prepareUI];
    }
    return self;
}

- (void)dealloc
{
    _danmakuTimer = nil;
}

- (void)prepareUI
{
    self.layer.masksToBounds = YES;
    
    _titleImageView = [[FQDLabTitleImageView alloc] init];
    [self.contentView addSubview:_titleImageView];
    
    WeakSelf;
    [_titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.contentView);
    }];
    
    [self prepareDanmakuView];
}

- (void)prepareDanmakuView
{
    CGFloat top = 30.0f;
    CGRect rect =  CGRectMake(0, top, CGRectGetWidth(self.bounds), [[self class] cellHeight]-top);
    DanmakuConfiguration *configuration = [[DanmakuConfiguration alloc] init];
    configuration.duration = 8;
    configuration.paintHeight = 45;
    configuration.fontSize = 15;
    configuration.largeFontSize = 17;
    configuration.maxLRShowCount = 6;
    configuration.maxShowCount = 70;
    self.danmakuConfig = configuration;
    
    _danmakuView = [[DanmakuView alloc] initWithFrame:rect configuration:configuration];
    _danmakuView.delegate = self;
    [_titleImageView insertSubview:_danmakuView belowSubview:_titleImageView.joinCountView];
    
}

- (void)startDanmaku
{
    if (self.danmakuView.isPrepared) {
        [self.danmakuTimer invalidate];
        self.danmakuTimer = nil;
        if (!self.danmakuTimer) {
            self.danmakuTimer = [NSTimer timerWithTimeInterval:0.1 target:self
                                                      selector:@selector(tick) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:self.danmakuTimer forMode:NSRunLoopCommonModes];
        }
        [self.danmakuView start];
    }
}

- (void)stopDanmaku
{
    [self.danmakuView stop];
    [self.danmakuTimer invalidate];
    self.danmakuTimer = nil;
    self.currentTime = 0;
}

- (void)tick
{
    self.currentTime += 0.1;
    if (self.currentTime > self.maxTime) {
        self.currentTime = 0;
    }
}

#pragma mark - DanmakuDelegate
- (float)danmakuViewGetPlayTime:(DanmakuView *)danmakuView
{
    return self.currentTime;
}

- (BOOL)danmakuViewIsBuffering:(DanmakuView *)danmakuView
{
    return NO;
}

- (void)danmakuViewPerpareComplete:(DanmakuView *)danmakuView
{
    [self startDanmaku];
}

#pragma mark - Getters and Setters
- (void)setPost:(FQDPost *)post
{
    _post = post;
    self.titleImageView.post = post;
}

- (void)setOpinions:(NSArray *)opinions
{
    _opinions = opinions;
    
    NSMutableArray *danmakus = [NSMutableArray arrayWithCapacity:[opinions count]];
    NSInteger startTime = 0;
    for (FQDLabOpinion *opinion in opinions) {
        NSString *p = [NSString stringWithFormat:@"%@,0,0,FFFFFF,0", @(startTime)];
        DanmakuSource *source = [DanmakuSource createWithP:p O:opinion];
        [danmakus addObject:source];
        
        startTime += arc4random_uniform(1500);
    }
    self.maxTime = startTime/1000.0f + self.danmakuConfig.duration;
    [self.danmakuView prepareDanmakuSources:[danmakus copy]];
    [self startDanmaku];
}

+ (CGFloat)cellHeight
{
    return 300.0f;
}

+ (NSString *)reuseId
{
    return @"FQDLabOpinionHeadCell";
}

@end
